/*
* archdep.h Things that change depending on the archetecture
* Copyright 2006-2008 OpenWii (admin@openwii.org)
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
*
* Chiip (ATmega8L8) Flavor - eris
*
* Version 2.4 by Andii
*/

#ifndef ARCHDEP_H
#define ARCHDEP_H

// other files to include for this micro
#include <avr/io.h>
#include <avr/sfr_defs.h>
#include <avr/interrupt.h>
#include <avr/pgmspace.h>
#include <avr/eeprom.h>
#include <inttypes.h>

// These may want to be changed to just PCx instead of teh _BV(PCx) for easier switching
#define PIN_CLK (_BV(PC0))	// PORT MASK FOR CLK PIN
#define PIN_OUT (_BV(PC2))	// PORT MASK FOR OUTPUT PIN
#define PIN_IN	(_BV(PC1))	// PORT MASK FOR INPUT PIN

// macros to change the output pin states
#define OUTHIZ DDRC &= ~PIN_OUT	// Output HiZi
#define OUTLOW DDRC |= PIN_OUT	// Output Low
#define CLKHIZ DDRC &= ~PIN_CLK	// clock HiZ
#define CLKLOW DDRC |= PIN_CLK	// Drive clock low

// macro to read input pin as 0 or 1 (see note about about chaging pinname usage)
#define GETIN (((PINC & PIN_IN) >> PC1) & 0x1)	// read input

// Macros for LED
#define LEDPIN	(_BV(PC3))
#define LEDOFF	PORTC&=~LEDPIN
#define LEDON	PORTC|=LEDPIN
#define LEDTOG	PORTC^=LEDPIN

// Macros to define switch input
#define SW0PIN	(_BV(PD5))
#define SW0IN	((PIND >> 5) & 0x1)

// Define the clock feq for avr
#define F_CPU 4000000
#define USART_BAUD 9600		// setup the uart just in case
#define USART_UBBR_VALUE ((F_CPU / (USART_BAUD << 4)) - 1)

// Stuff used by the timer
#define USTIMERVAL 1000000	// 1 / 0.000001
#define MSTIMERVAL 1000		// 1 / 0.001
#define USTIMERDIV ((F_CPU / USTIMERVAL) - 1)
#define MSTIMERDIV ((F_CPU / MSTIMERVAL) - 1)

// These are just macros to define what functions to use for eeprom and program memory access
#define nvReadByte eeprom_read_byte
#define nvWriteByte eeprom_write_byte
#define pgmReadWord pgm_read_word

// Atmega8 locations for bootloader (must match .lds file)
#define PAGESIZE	64				// in bytes and not in word
#define APPLADDR	0				// application address
#define MIRRORADDR	3584			// update mirror address
#define MAXFIRMLEN	3580			// max. length of firmware
#define BOOTLADDR	7168			// bootloader address

#endif	// ARCHDEP_H
